package com.hualan.sfzy.service.course.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.entity.SysUser;
import com.hualan.sfzy.mapper.sysUser.SysUserMapper;
import com.hualan.sfzy.service.SysUserService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public int add(SysUser entity) {
        return sysUserMapper.insert(entity);
    }
    @Override
    public Result login(String username, String userpwd) {
        SysUser sysUser = sysUserMapper.selectSysUserByUsername(username);
        if (sysUser != null) {
            if(sysUser.getUserpwd().equals(userpwd)) {
                return Result.success(sysUser);
            }else{
                return Result.error("密码错误");
            }
        }
        return Result.error("用户不存在");
    }

    @Override
    public int edit(SysUser entity) {
        return sysUserMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        return sysUserMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return sysUserMapper.deleteBatch(ids);
    }

    @Override
    public List<SysUser> find() {
        return sysUserMapper.select(null);
    }
    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.selectAll();
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, SysUser entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysUser> sysUsers = sysUserMapper.select(entity);
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }

    @Override
    public SysUser findById(Integer id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public SysUser findByUsername(String username) {
        return sysUserMapper.selectSysUserByUsername(username);
    }

}
